<?php

namespace app\helper;

use fast\Random;

class Helper
{
    const PI=3.1415926535898;
    const EARTH_RADIUS=6378.137;
    public static function generateInviteCode()
    {
        $i = 0;
        do {
            if($i>1){
                return false;
            }
            $inviteCode = strtoupper(Random::alnum(8));
            $i++;
        }while(self::generateInviteCode($inviteCode));
        return $inviteCode;
    }

    public static function orderNo($prefix = 'P',$order_id='00000')
    {
        return $prefix.str_pad($order_id,5,'0',STR_PAD_LEFT).date('ymdHi') . rand(1000, 9999);

    }

    /**
     * 求两点距离
     * @param $lat1
     *
     * @param $lng1
     * @param $lat2
     * @param $lng2
     * @return float|int
     */
   public static function getDistance($lat1, $lng1, $lat2, $lng2){
        $radLat1 = $lat1 * (self::PI / 180);
        $radLat2 = $lat2 * (self::PI / 180);
        $a = $radLat1 - $radLat2;
        $b = ($lng1 * (self::PI / 180)) - ($lng2 * (self::PI / 180));
        $s = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1)*cos($radLat2)*pow(sin($b/2),2)));
        $s = $s * self::EARTH_RADIUS;
        return round($s * 10000) / 10000;
    }
}